91B - Queue - CodeForces Solution


binary search data structures *1500

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <math.h>
//in the name of god,aka allah
//**sarv chaman man chera meyl chaman nemikonad**
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
#define pi pair<long long , long long>
#define pii pair<long long , pair<long long , long long>>
const int maxm = 5e5 + 3;
const long long mod =1e9 + 7;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
ll l,r,mid;
ll n,m;
ll dis[maxm] , sum[maxm];
bool isval(int mid){
    //cout << mid <<" " << mid*mid-mid <<endl;
    if (((mid-1)*mid)/2 < m) return 0;
    return 1;
}
ll darage[maxm] , ss , mm;
queue<int> q;
vector<pi> g[maxm] , z[maxm];
ll sath[maxm];
bool vis[maxm] , gos[maxm];
ll pedaret[maxm];
ll get_par(ll v){
    if (pedaret[v]==v) return v;
    return pedaret[v] = get_par(pedaret[v]);
}
void merge(ll r , ll q){
    if (get_par(r)!=get_par(q))l+=max(darage[r],darage[q])*1ll*sath[r]*1ll*sath[q];
    r = get_par(r) , q = get_par(q);
    if (r!=q){
        if (sath[r]<sath[q]) swap(r,q);
        pedaret[q] = r;
        sath[r] += sath[q];
    }
    return ;
}
ll pars1[maxm] , pars2[maxm];
vector<ll> se[maxm];
multiset<int> st;
ll rp[maxm];
pi w[maxm];
ll dp[maxm * 2];
//ll rw[maxm][maxm];
map<ll,ll> mp;
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin >>n;
    set<pi> st;
    for (int i=1; i<=n; i++){
        cin>>pedaret[i];
        st.insert({pedaret[i],i});
    }   
    for (int i=n; i; i--){
        if (!vis[i]){
            darage[i] = -1;
            while (st.size() && (*st.rbegin()).fi>pedaret[i]){
                vis[(*st.rbegin()).se] = 1;
                darage[(*st.rbegin()).se] = i-(*st.rbegin()).se-1;
                st.erase(*st.rbegin());
            }
            st.erase({pedaret[i],i});
        }
    }
    for (int i=1; i<=n; i++) cout<<darage[i]<<" ";
}


Comments

Submit
0 Comments
More Questions

20. Valid Parentheses
746. Min Cost Climbing Stairs
392. Is Subsequence
70. Climbing Stairs
53. Maximum Subarray
1527A. And Then There Were K
1689. Partitioning Into Minimum Number Of Deci-Binary Numbers
318. Maximum Product of Word Lengths
448. Find All Numbers Disappeared in an Array
1155. Number of Dice Rolls With Target Sum
415. Add Strings
22. Generate Parentheses
13. Roman to Integer
2. Add Two Numbers
515. Find Largest Value in Each Tree Row
345. Reverse Vowels of a String
628. Maximum Product of Three Numbers
1526A - Mean Inequality
1526B - I Hate 1111
1881. Maximum Value after Insertion
237. Delete Node in a Linked List
27. Remove Element
39. Combination Sum
378. Kth Smallest Element in a Sorted Matrix
162. Find Peak Element
1529A - Eshag Loves Big Arrays
19. Remove Nth Node From End of List
925. Long Pressed Name
1051. Height Checker
695. Max Area of Island